﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using InnovTech.Util.Crypto;
using InnovTech.Util.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace InnovTech.Util.Test
{
    [TestClass]
    public class TestCryptoUtil
    {
        [TestMethod]
        public void TestCreateCrypto()
        {
            Guid id = Guid.NewGuid();
            //SymmetricAlgorithm provider = CryptoUtil.CreateCryptoProvider<TripleDESCryptoServiceProvider>(
            //    CryptoUtil.GenerateBytes(id, new byte[] { 23 }, 24), CryptoUtil.GenerateBytes(id, new byte[] { 23 }, 8));

            SymmetricAlgorithm provider = CryptoUtil.CreateCryptoProvider<TripleDESCryptoServiceProvider>();

            String toTest = Utf8Util.CreateCharSets();

            String result = null;

            using (MemoryStream input = new MemoryStream(Encoding.UTF8.GetBytes(toTest)))
            {
                using (MemoryStream encryptStream = new MemoryStream())
                {
                    using (CryptoStream cs = CryptoUtil.Crypt(input, provider.CreateEncryptor(), encryptStream))
                    {
                        byte[] saved = encryptStream.ToArray();
                        using (MemoryStream decryptStream = new MemoryStream())
                        using (CryptoStream dcs = CryptoUtil.Crypt(encryptStream, provider.CreateDecryptor(), decryptStream))
                        {
                            byte[] bytes = decryptStream.ToArray();
                            result = Encoding.UTF8.GetString(bytes);
                        }
                    }

                    
                }
                
            }

            Assert.AreEqual(toTest, result);
            
        }

        [TestMethod]
        public void TestEncryptString()
        {
            String toTest = Utf8Util.CreateCharSets();
            byte[] encrypted = CryptoUtil.Encrypt(toTest);
            String decrypted = CryptoUtil.Decrypt(encrypted);
            Assert.AreEqual(toTest, decrypted);

            Guid seed = Guid.NewGuid();
            SymmetricAlgorithm provider = CryptoUtil.CreateCryptoProvider<TripleDESCryptoServiceProvider>(
                CryptoUtil.GenerateBytes(seed, new byte[] { 23 }, 24), CryptoUtil.GenerateBytes(seed, new byte[] { 23 }, 8));
            encrypted = null;
            decrypted = null;
            encrypted = CryptoUtil.Encrypt(toTest, provider);
            decrypted = CryptoUtil.Decrypt(encrypted, provider);
            Assert.AreEqual(toTest, decrypted);
        }

        [TestMethod]
        public void TestEncryptStringToBase64()
        {
            String toTest = Utf8Util.CreateCharSets();
            string base64 = CryptoUtil.EncryptToBase64String(toTest);
            String decrypted = CryptoUtil.DecryptFromBase64String(base64);
            Assert.AreEqual(toTest, decrypted);

            Guid seed = Guid.NewGuid();
            SymmetricAlgorithm provider = CryptoUtil.CreateCryptoProvider<TripleDESCryptoServiceProvider>(
                CryptoUtil.GenerateBytes(seed, new byte[] { 23 }, 24), CryptoUtil.GenerateBytes(seed, new byte[] { 23 }, 8));
            base64 = null;
            decrypted = null;
            base64 = CryptoUtil.EncryptToBase64String(toTest, provider);
            decrypted = CryptoUtil.DecryptFromBase64String(base64, provider);
            Assert.AreEqual(toTest, decrypted);
        }
    }
}
